//
// Created by Administrator on 2023/11/16.
//
#include "iostream"
#include "cstring"

using namespace std;

const int N = 1e5 + 10;
int n, m;
int h[N], e[N], ne[N], idx;

bool st[N];
int q[N];//queue
int d[N];//distance

void add(int a, int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx;
    idx++;
}

int bfs() {
    int hh = 0, tt = 0;//head & tail
    q[tt++] = 1; //init the first node:id==1
    d[1] = 0;

    //queue is not empty
    while (hh != tt) {
        int t = q[hh++]; //queue pop
        if (hh == N) hh = 0;// connect with the below judge ,can make a cycle queue
        if (t == n)return d[n]; //end advance [the id of the end node is n]

        // move to every possible direction
        for (int i = h[t]; i != -1; i = ne[i]) {
            int j = e[i];//'e' is the end of edge, 'j' is the end node of the edge
            if (d[j] == -1) {// if 'j' haven't visited
                d[j] = d[t] + 1; // add distance
                q[tt++] = j;//enqueue
                if (tt == N) tt = 0;
            }
        }

    }
    return -1;
}

int main() {
    cin >> n >> m;
    int a, b;

    memset(h, -1, sizeof h);// head node array
    memset(d, -1, sizeof d);//also act as flag

    while (m--) {
        cin >> a >> b;
        add(a, b);
    }
    cout<<bfs();
}